package com.jhws.sys.appUpgrade.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;

import com.jhws.sys.appUpgrade.bo.TblSoftLabelDimBo;
import com.jhws.common.Page;

@Transactional
public class TblSoftLabelDimHibernateDAO implements ITblSoftLabelDimDAO {
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public TblSoftLabelDimBo getById(String softLabelCode) {
		
		Query q = sessionFactory.getCurrentSession().createQuery("from TblSoftLabelDimBo where soft_label_code = ?");
		q.setParameter(0, softLabelCode);
		List<TblSoftLabelDimBo> list = q.list();
		if (list.size() != 0)
			return (TblSoftLabelDimBo) list.get(0);
		else
			return null;
	}
	
	public TblSoftLabelDimBo getSoftLabelById(String softLabelCode) {
		return this.getById(softLabelCode);
	}

	public Page findTblSoftLabelDim(int start, int num, String whereClause)
			throws Exception {
		try {
			StringBuffer buf = new StringBuffer();
			buf.append("from TblSoftLabelDimBo ");
			buf.append(whereClause);
			buf.append("order by soft_labe_name ");
			  
			Query query = sessionFactory.getCurrentSession().createQuery(buf.toString());
			query.setFirstResult(start);
			query.setMaxResults(num);
			List list = query.list();

			Page page = new Page(start, num, list);
			String hql = "select count(*) from TblSoftLabelDimBo "+ (whereClause == null || "".equals(whereClause.trim()) ? "" : whereClause.trim());
			query = sessionFactory.getCurrentSession().createQuery(hql);
			page.setTotalNumber(((Number)query.uniqueResult()).intValue());
			return page;
		} catch (Exception e) {
			System.out.println("=========e:"+e.getMessage());
			return new Page();
		}
	}

	public TblSoftLabelDimBo save(TblSoftLabelDimBo bo) {
		sessionFactory.getCurrentSession().saveOrUpdate(bo);
		return bo;
	}

	public List<TblSoftLabelDimBo> findTblSoftLabelDimByWhere(String whereClause) throws Exception {
		try {
			StringBuffer buf = new StringBuffer("from TblSoftLabelDimBo ");
			if(whereClause != null && !"".equals(whereClause))
				buf.append(whereClause);
			buf.append(" order by soft_labe_name ");
			Query query = sessionFactory.getCurrentSession().createQuery(buf.toString());
			
			@SuppressWarnings("unchecked")
			List<TblSoftLabelDimBo> list = query.list();
			return list;
		} catch (Exception e) {
			return new ArrayList<TblSoftLabelDimBo>();
		}
	}

	public void deleteTblSoftLabelDim(String softLabelCode) {
		TblSoftLabelDimBo bo = getSoftLabelById(softLabelCode);
		if (null != bo)
		{
			sessionFactory.getCurrentSession().delete(bo);
		}
	}


	public TblSoftLabelDimBo getTblSoftLabel(TblSoftLabelDimBo bo) {
		Query q = sessionFactory.getCurrentSession().createQuery("from TblSoftLabelDimBo where soft_labe_name = ?");
		q.setParameter(0, new Object[]{bo.getSoftLabeName()});
		List list = q.list();
		
		if(list.size() != 0)
			return (TblSoftLabelDimBo) list.get(0);
		else 
			return null;
	}

}
